package com.weipu.common.facade.impl;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.weipu.common.constants.Constant;
import com.weipu.common.facade.SearchServiceProvider;
import com.weipu.common.facade.exception.DxException;

public class SearchServiceProviderImpl extends BaseServiceProviderImpl
		implements SearchServiceProvider {

	@Override
	public String[] getHotSearchWords() throws DxException {
		String[] hots = null;
		JSONObject data = requestHttpGetData(Constant.GETHOTSEARCHWORDS_URL);
		Log.i("Search", "getHotSearchWords"+data.toString());
		JSONArray hotSearchWords = data.optJSONArray("HotSearchWords");
		if (null != hotSearchWords) {
			hots = new String[hotSearchWords.length()];
			for (int i = 0; i < hotSearchWords.length(); i++) {
				try {
					hots[i] = hotSearchWords.getString(i);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			return hots;
		}

		return null;
	}

	@Override
	public String[] getIntellisenseWords(String searchKey) throws DxException {
		String[] hots = null;
		JSONObject data = requestHttpGetData(Constant.GETINTELLISENSEWORDS_URL
				+ "?Word=" + searchKey);
		Log.i("Search", "getIntellisenseWords"+data.toString());
		JSONArray hotSearchWords = data.optJSONArray("IntellisenseWords");
		if (null != hotSearchWords) {
			hots = new String[hotSearchWords.length()];
			for (int i = 0; i < hotSearchWords.length(); i++) {
				try {
					hots[i] = hotSearchWords.getString(i);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			return hots;
		}

		return null;
	}

}
